//
//  JZAlbumViewController.m
//  aoyouHH
//
//  Created by jinzelu on 15/4/27.
//  Copyright (c) 2015年 jinzelu. All rights reserved.
//

#import "JZAlbumViewController.h"
#import "UIImageView+WebCache.h"
#import "MBProgressHUD.h"
#import "PhotoView.h"

#define screen_width [[UIScreen mainScreen] bounds].size.width

#define screen_height [[UIScreen mainScreen] bounds].size.height

@interface JZAlbumViewController ()<UIScrollViewDelegate,PhotoViewDelegate>
{
   
    MBProgressHUD *HUD;
    NSMutableArray *_subViewList;
    PhotoView *_currentP;

}
@property(nonatomic,strong)PhotoView *zoomScroll;

@end

@implementation JZAlbumViewController

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        //
        _subViewList = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor blackColor];

    [self initScrollView];
    [self addLabels];
    [self setPicCurrentIndex:self.currentIndex];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)initScrollView{

    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, screen_width, screen_height)];
    self.scrollView.pagingEnabled = YES;
    self.scrollView.userInteractionEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    
    self.scrollView.contentSize = CGSizeMake(self.imgArr.count*screen_width, screen_height);
    self.scrollView.delegate = self;
    self.scrollView.contentOffset = CGPointMake(_currentIndex *screen_width, 0);

    
    [self.view addSubview:self.scrollView];
    
    for (int i = 0; i < self.imgArr.count; i++) {
        //url数组
        CGRect frame = CGRectMake(i*_scrollView.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);
        PhotoView *photoV = nil;
        if ([_imgArr[i] isKindOfClass:[UIImage class]]) {
            photoV = [[PhotoView alloc] initWithFrame:frame withPhotoImage:_imgArr[i]];
            
        } else {
            photoV = [[PhotoView alloc] initWithFrame:frame withPhotoUrl:_imgArr[i]];
        }
        
        photoV.delegate = self;
        [self.scrollView addSubview:photoV];
        NSLog(@"%d--%@", i, photoV);
        if (i == _currentIndex) {
            //用属性zoomScroll保存第一个小scrollview
            _currentP = photoV;
        }
        
    }

}

-(void)addLabels{
    self.sliderLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, screen_height-64-49, 60, 30)];
    self.sliderLabel.backgroundColor = [UIColor clearColor];
    self.sliderLabel.textColor = [UIColor whiteColor];
    self.sliderLabel.text = [NSString stringWithFormat:@"%ld/%lu",self.currentIndex+1,(unsigned long)self.imgArr.count];
    [self.view addSubview:self.sliderLabel];
}

-(void)setPicCurrentIndex:(NSInteger)currentIndex{
    _currentIndex = currentIndex;
    self.scrollView.contentOffset = CGPointMake(screen_width*currentIndex, 0);

}


#pragma mark - PhotoViewDelegate
-(void)TapHiddenPhotoView{

    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)OnTapView{
    [self dismissViewControllerAnimated:YES completion:nil];
}


#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSLog(@"scrollViewDidEndDecelerating");
    int i = scrollView.contentOffset.x/screen_width+1;
//    [self loadPhote:i-1];
    self.sliderLabel.text = [NSString stringWithFormat:@"%d/%lu",i,(unsigned long)self.imgArr.count];
    
    
    //self.currentIndex记录的是当前小点从0开始.但如果取下标为0的就是最后一张,所以需要+1取第一张
    PhotoView *scrollview1 = [scrollView.subviews objectAtIndex:i-1];
//    NSLog(@"这是第几张:%lf / %lf = %lf", scrollView.contentOffset.x, screen_width, scrollView.contentOffset.x/screen_width);
    
    //如果保存的小scrollview不是用户缩放的小scrollview,则让其复原,并重新记录用户缩放的小scrollview
    if (_currentP != scrollview1 && [scrollview1 isKindOfClass:[PhotoView class]]) {
        [_currentP scrollViewZoomOrgWithScrollView:_currentP.scrollView];
        _currentP = scrollview1;
//        NSLog(@"重新记录是%d--%@", i-1, _currentP);
    }
    
    
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
